/*----------------------------------------------------------------------------
 * osObjects.h: CMSIS-RTOS global object definitions for an application
 *----------------------------------------------------------------------------
 *
 * This header file defines global RTOS objects used throughout a project
 *
 * #define osObjectsPublic indicates that objects are defined; without that
 * definition the objects are defined as external symbols.
 *
 *--------------------------------------------------------------------------*/


#ifndef __osObjects
#define __osObjects


#ifdef osObjectsCreat
#define OBJ_EXT  
#else
#define OBJ_EXT extern 
#endif


#include "cmsis_tx.h"
#include "common.h"
     
//#include "cmsis_os.h"               // CMSIS RTOS header file

/*******************************************************************************
* @brief  时基
*******************************************************************************/
#define OS_TICK_FREQ       TX_TIMER_TICKS_PER_SECOND
#define ms_ticks(x)        ((x*OS_TICK_FREQ+999)/1000)


/*******************************************************************************
* @brief  API 调试信息
*******************************************************************************/
extern char * tx_err_txt[];

#if (DBG_EN+OS_API_DBG_EN) > 1
void os_err_print(uint16_t os_err, char *file, int line);
#define os_api_exe_check() os_err_print(os_err, __FILE__, __LINE__)
#else
#define os_api_exe_check() (void) os_err
#endif

/*******************************************************************************
* @brief  接口
*******************************************************************************/
int os_thread_init(void);
int os_obj_creat(void);
int os_thread_creat (void);

/*******************************************************************************
*                       任务优先级，数值越小优先级越高
*******************************************************************************/
#define  APP_CFG_TASK_START_PRIO                          2u
#define  APP_CFG_TASK_USER_IF_PRIO                        4u
#define  APP_CFG_TASK_STAT_PRIO                           13u
#define  APP_CFG_TASK_IDLE_PRIO                           31u

#define  APP_CFG_TASK_CONSOLE_PRIO                        13u
#define  APP_CFG_TASK_SYSTEM_PRIO                         3u
#define  APP_CFG_TASK_OSAPI_PRIO                          25u
#define  APP_CFG_TASK_MODBUS_PRIO                         5u
#define  APP_CFG_TASK_FILEX_PRIO                          22u


/*******************************************************************************
*                                静态全局变量
*******************************************************************************/
typedef struct
{
	TX_THREAD   tcb_app_task_start;
	uint64_t    stk_app_task_start[1024 / 8];

	TX_THREAD   tcb_task_idle;
	uint64_t    stk_task_idle[256 / 8];

	TX_THREAD   tcb_task_stat;
	uint64_t    stk_task_stat[512 / 8];

	TX_THREAD   tcb_task_console;
	uint64_t    stk_task_console[1024 / 8];

	TX_THREAD   tcb_task_system;
	uint64_t    stk_task_system[8*1024 / 8];
	osThreadId  tid_system;
    
    TX_THREAD   tcb_task_modbus;
	uint64_t    stk_task_modbus[512 / 8];
	osThreadId  tid_modbus;

    TX_THREAD   tcb_task_filex;
    osThreadId  tid_filex;
    
	TX_MUTEX   mutex_printf;	/* 用于printf互斥 */
    osMutexId  mid_printf;
    
	TX_MUTEX   mutex_common;	/* 用于全局公共资源保护 */
    osMutexId  mid_common; 
    
	TX_MUTEX   mutex_mem;	/* mem */
    osMutexId  mid_mem; 
	
	TX_QUEUE   quene_tst;
	uint32_t   quene_tst_mem[32];
    osQueneId  qid_tst;
    
    TX_EVENT_FLAGS_GROUP event_mb;
    osEventId  eid_mb;
	
    TX_EVENT_FLAGS_GROUP event_can;
    osEventId  eid_can;
	
	TX_SEMAPHORE sem_tst;
    osSemaphoreId sid_tst;
    
	// thread id
	osThreadId  tid_StorageTask;                              // thread id
	osThreadId  tid_KeyTask;                                  // thread id
	osThreadId  tid_console;

	// global 'semaphores' -----------------------------------------------------
	osMutexId mid_q25;
	
	// global 'message queues' -------------------------------------------------


	// global 'mail queues' ----------------------------------------------------

	// mem
	TX_BYTE_POOL mem_pool;
	TX_BYTE_POOL * pid_mem;
} os_obj_type;

OBJ_EXT os_obj_type  os_obj;                                  // thread id

int32_t os_task_stk_used(osThreadId tid);
void tx_task_info(void);

#endif  // __osObjects
